#include <dspSimulator/instruction/cmpgt.h>

bool CMPGT::checkCondition(std::shared_ptr<DspMachine> dspMachine) {
    if (isCompactInstruction) {
        return true;
    } else {
        return checkStandardCondition(dspMachine);
    }
}

void CMPGT::loadInstIRIntoJit(llvm::LLVMContext *llvmContext, llvm::Module *module,
                              std::shared_ptr<llvm::IRBuilder<>> irBuilder) {
    llvm::FunctionType *funcType = llvm::FunctionType::get(irBuilder->getVoidTy(), false);
    // --------------------------------- cmpgtu (.unit) uint,xuint, uint---------------------------------------------
    {
        llvm::Function *cmpltuL = llvm::Function::Create(funcType, llvm::GlobalValue::ExternalLinkage, "CMPGTKind1",
                                                         module);
        llvm::BasicBlock *entryBB = llvm::BasicBlock::Create(*llvmContext, "entry", cmpltuL);
        llvm::BasicBlock *thenBB = llvm::BasicBlock::Create(*llvmContext, "then", cmpltuL);
        llvm::BasicBlock *elseBB = llvm::BasicBlock::Create(*llvmContext, "else", cmpltuL);
        llvm::BasicBlock *endBB = llvm::BasicBlock::Create(*llvmContext, "end", cmpltuL);
        irBuilder->SetInsertPoint(entryBB);

        auto dstRegPtr = getDstLoRegPtr(module, irBuilder);

        auto src2RegPtr = getSrc2LoRegPtr(module, irBuilder);
        auto src2 = irBuilder->CreateLoad(src2RegPtr);

        auto src1RegPtr = getSrc1LoRegPtr(module, irBuilder);
        auto src1 = irBuilder->CreateLoad(src1RegPtr);

        auto resPtr = irBuilder->CreateAlloca(irBuilder->getInt32Ty());

        auto cond = irBuilder->CreateICmpSGT(src1, src2);
        irBuilder->CreateCondBr(cond, thenBB, elseBB);

        irBuilder->SetInsertPoint(thenBB);
        irBuilder->CreateStore(irBuilder->getInt32(1), resPtr);
        irBuilder->CreateBr(endBB);

        irBuilder->SetInsertPoint(elseBB);
        irBuilder->CreateStore(irBuilder->getInt32(0), resPtr);
        irBuilder->CreateBr(endBB);

        irBuilder->SetInsertPoint(endBB);
        auto res = irBuilder->CreateLoad(resPtr);
        irBuilder->CreateStore(res, dstRegPtr);
        irBuilder->CreateRetVoid();
    }
    // --------------------------------- cmpgtu (.unit) scst5,xsint, uint---------------------------------------------
    {
        llvm::Function *cmpltuL = llvm::Function::Create(funcType, llvm::GlobalValue::ExternalLinkage, "CMPGTKind2",
                                                         module);
        llvm::BasicBlock *entryBB = llvm::BasicBlock::Create(*llvmContext, "entry", cmpltuL);
        llvm::BasicBlock *thenBB = llvm::BasicBlock::Create(*llvmContext, "then", cmpltuL);
        llvm::BasicBlock *elseBB = llvm::BasicBlock::Create(*llvmContext, "else", cmpltuL);
        llvm::BasicBlock *endBB = llvm::BasicBlock::Create(*llvmContext, "end", cmpltuL);
        irBuilder->SetInsertPoint(entryBB);

        auto dstRegPtr = getDstLoRegPtr(module, irBuilder);

        auto src2RegPtr = getSrc2LoRegPtr(module, irBuilder);
        auto src2 = irBuilder->CreateLoad(src2RegPtr);

        auto cst5 = getCstN(module, irBuilder, 5);
        auto scst5 = irBuilder->CreateSExt(cst5, irBuilder->getInt32Ty());

        auto resPtr = irBuilder->CreateAlloca(irBuilder->getInt32Ty());

        auto cond = irBuilder->CreateICmpSGT(scst5, src2);
        irBuilder->CreateCondBr(cond, thenBB, elseBB);

        irBuilder->SetInsertPoint(thenBB);
        irBuilder->CreateStore(irBuilder->getInt32(1), resPtr);
        irBuilder->CreateBr(endBB);

        irBuilder->SetInsertPoint(elseBB);
        irBuilder->CreateStore(irBuilder->getInt32(0), resPtr);
        irBuilder->CreateBr(endBB);

        irBuilder->SetInsertPoint(endBB);
        auto res = irBuilder->CreateLoad(resPtr);
        irBuilder->CreateStore(res, dstRegPtr);
        irBuilder->CreateRetVoid();
    }

}

std::shared_ptr<Instruction> CMPGT::decode(std::shared_ptr<InstInfo> instInfo, u64 baseCycle) {
    std::shared_ptr<CMPGT> cmpgt;
    auto dspInstInfo = std::dynamic_pointer_cast<DspInstInfo>(instInfo);
    bool isStandardInst = dspInstInfo->isStandardInstruction();
    if (isStandardInst) {
        auto data = vectorToBitSet<32>(dspInstInfo->getBits());
        if (checkBits<10>(data, 2, 0b1000111110) && existStandardCondition(data)) {
            cmpgt = std::make_shared<CMPGT>(dspInstInfo->getAddress(), baseCycle);
            cmpgt->creg_z = extractBits<4>(data, 28);
            cmpgt->dst = extractBits<5>(data, 23).to_ulong();
            cmpgt->src2 = extractBits<5>(data, 18).to_ulong();
            cmpgt->src1 = extractBits<5>(data, 13).to_ulong();
            cmpgt->x = data[12];
            cmpgt->s = data[1];
            cmpgt->p = data[0];
            if (cmpgt->s == 0) {
                cmpgt->funcUnit = FuncUnit::L1;
            } else {
                cmpgt->funcUnit = FuncUnit::L2;
            }
            cmpgt->instFormat = InstFormat::Kind1;
        } else if (checkBits<10>(data, 2, 0b1000110110) && existStandardCondition(data)) {
            cmpgt = std::make_shared<CMPGT>(dspInstInfo->getAddress(), baseCycle);
            cmpgt->creg_z = extractBits<4>(data, 28);
            cmpgt->dst = extractBits<5>(data, 23).to_ulong();
            cmpgt->src2 = extractBits<5>(data, 18).to_ulong();
            cmpgt->cst = extractBits<5>(data, 13).to_ulong();
            cmpgt->x = data[12];
            cmpgt->s = data[1];
            cmpgt->p = data[0];
            if (cmpgt->s == 0) {
                cmpgt->funcUnit = FuncUnit::L1;
            } else {
                cmpgt->funcUnit = FuncUnit::L2;
            }
            cmpgt->instFormat = InstFormat::Kind2;
        }
    } else {
        auto data = vectorToBitSet<16>(dspInstInfo->getBits());
        if (data[11] == 1 && data[10] == 1 && checkBits<2>(data, 5, 0b01) && checkBits<3>(data, 1, 0b100)) {
            cmpgt = std::make_shared<CMPGT>(dspInstInfo->getAddress(), baseCycle);
            cmpgt->isCompactInstruction = true;
            cmpgt->compactInstKind = "L2c";
            cmpgt->src1 = extractBits<3>(data, 13).to_ulong();
            cmpgt->x = data[12];
            cmpgt->src2 = extractBits<3>(data, 7).to_ulong();
            cmpgt->dst = extractBits<1>(data, 4).to_ulong();
            cmpgt->s = data[0];
            if (cmpgt->s == 0) {
                cmpgt->funcUnit = FuncUnit::L1;
            } else {
                cmpgt->funcUnit = FuncUnit::L2;
            }
            cmpgt->DSZ = dspInstInfo->getDSZ();
            cmpgt->RS = dspInstInfo->getRS();
            cmpgt->SAT = dspInstInfo->getSAT();
            cmpgt->PROT = dspInstInfo->getPROT();
            cmpgt->BR = dspInstInfo->getBR();
            cmpgt->p = dspInstInfo->getP();
            cmpgt->fixUpRegOnCompactInstruction();
            cmpgt->instFormat = InstFormat::Kind1;
        }
    }
    return cmpgt;
}

void CMPGT::executeCustom(std::shared_ptr<TargetMachine> targetMachine, ExecutePhase executePhase) {

}

std::string CMPGT::toString() const {
    std::string res;
    if (!isCompactInstruction) {
        res += getStandardConditionString() + " ";
    }
    res += m_name + " " + FuncUnitName[static_cast<u32>(funcUnit)] + " ";
    if (instFormat == InstFormat::Kind1) {
        res += getSrc1Reg32Name() + "," + getSrc2Reg32Name() + "," + getDstReg32Name();
    } else if (instFormat == InstFormat::Kind2) {
        res += std::to_string(extractBitsAndToInt<5>(cst)) + "," + getSrc2Reg32Name() + "," + getDstReg32Name();
    }
    return res;
}

std::shared_ptr<DspInstruction> CMPGT::clone() {
    return std::make_shared<CMPGT>(*this);
}
